<?php

/**
 * Yii Controller SearchController
 *
 * @author trongkhoi
 * @since Oct 7, 2013 - 7:41:39 PM
 */
class FavouriteController extends FrontController
{

    public $layout = '//layouts/browser';

    protected function criteria()
    {
        $ret = new CDbCriteria();
        $ukey = Users::getUkeyById(Yii::app()->user->getId());

        $favourite_model = Favourite::model()->findAll('ukey=:u', array(
            ':u' => $ukey
        ));
        //array service id
        $service_ids = array();
        foreach ($favourite_model as $v)
        {
            $service_ids[] = $v['sid'];
        }

        $ret->addInCondition('sid', $service_ids);
        return $ret;
    }

    public function actionIndex()
    {
        Yii::app()->session['offset_gallery'] = 0;
        Yii::app()->session['offset_list'] = 0;

        $crite = $this->criteria();
        $crite->limit = $this->currentLimit;
        $crite->offset = 0;
        $model = Service::model()->findAll($crite);

        $this->render('index', array('model' => $model));
    }

    public function actionGallery()
    {
        $offset = Yii::app()->session['offset_gallery'] + $this->currentLimit;
//        $count = Service::model()->count();
//        if ($offset > $count)
//        {
//            $offset=Yii::app()->session['offset_favourite'];
//        }

        $crite = $this->criteria();
        $crite->limit = $this->currentLimit;
        $crite->offset = $offset;

        Yii::app()->session['offset_gallery'] = $offset;

        $model = Service::model()->findAll($crite);
        if ($model != NULL)
        {
            echo $this->renderPartial('_gallery', array('model' => $model));
            Yii::app()->end();
        }
        else
        {
            echo 'ok';
            Yii::app()->end();
        }
    }

    public function actionList()
    {
        $offset = Yii::app()->session['offset_list'] + $this->currentLimit;
//        $count = Service::model()->count();
//        if ($offset > $count)
//        {
//            $offset=Yii::app()->session['offset_favourite'];
//        }

        $crite = $this->criteria();
        $crite->limit = $this->currentLimit;
        $crite->offset = $offset;

        Yii::app()->session['offset_list'] = $offset;

        $model = Service::model()->findAll($crite);
        if ($model != NULL)
        {
            echo $this->renderPartial('_list', array('model' => $model));
        }
        else
        {
            echo 'ok';
        }
    }

    public function actionCheck()
    {
        $sid = $_POST['sid'];
        $type_service = isset($_POST['type']) ? FALSE : TRUE; //NẾU CÓ BIẾN  NÀY LÀ CHECK BIZ FAVO
        $ukey = Users::getUkeyById(Yii::app()->user->getId());

        //check đã user đã favo service hay merchant
        $check = $type_service ? Favourite::checkAdded($ukey, $sid) : Favourite::checkAdded($ukey, $sid, FALSE);
        if ($check == FALSE)
        {
            if ($type_service == TRUE)
            {
                if ($this->Add($ukey, $sid))
                {
                    echo json_encode(array(
                        'status' => 1
                    ));
                }
            }
            else
            {
                if ($this->Add($ukey, $sid, FALSE))
                {
                    echo json_encode(array(
                        'status' => 1
                    ));
                }
            }
        }
        else
        {
            if ($this->Remove(Favourite::checkAdded($ukey, $sid)))
            {
                echo json_encode(array(
                    'status' => 0
                ));
            }
        }
    }

    /**
     * 
     * @param type $ukey
     * @param type $sid service id hoặc merchant id
     * @param type $service TRUE nếu add favo service, FALSE nếu add favo merchant
     * @return boolean
     */
    private function Add($ukey, $sid, $service = TRUE)
    {
        $favourite = new Favourite();
        $favourite->ukey = $ukey;
        if ($service == TRUE)
        {
            $favourite->sid = $sid;
            $favourite->type = Favourite::TYPE_SERVICE;
        }
        else
        {
            $favourite->biz_id = $sid;
            $favourite->type = Favourite::TYPE_BIZ;
        }
        if ($favourite->save())
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    private function Remove($favou_id)
    {
        $favourite = Favourite::model()->findByPk($favou_id);
        if ($favourite->delete())
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    public function actionGetAllService()
    {
        //$service = Service::model()->findAll($this->criteria());
        $service = Service::model()->findAll();
        $result_arr = array();

        $ukey = Users::getUkeyById(Yii::app()->user->getId());
        foreach ($service as $sv)
        {
            if ($sv->longitude != '')
            {
                $icon_class = (!Yii::app()->user->isGuest && Favourite::checkAdded($ukey, $sv->sid)) ? '<i class="icon-heart-pink"></i>' : '<i class="icon-heart-white"></i>';
                $icon_title = Favourite::checkAdded($ukey, $sv->sid) ? Yii::t('main','Remove from My Favourites') : Yii::t('frontend','Add to Favourites');
                $avr_point = ServiceRate::getAvrPoint($sv->sid);
                $rate = $this->genArrayVote($avr_point, 5, $sv->sid);
                $vote_time = ServiceRate::getReviewTimes($sv->sid);
                $result_arr[] = array(
                    'sid' => $sv->sid,
                    'lng' => $sv->longitude,
                    'lat' => $sv->latitude,
                    'name' => $sv->s_name,
                    'price' => $sv->price,
                    'avatar' => $sv->publicAvatar,
                    'bookingurl' => $sv->bookingUrl,
                    'detailurl' => $sv->detailUrl,
                    'address' => $sv->address,
                    'bizname' => $sv->user->business_name,
                    'icon_class' => $icon_class,
                    'rate' => $rate,
                    'vote_time' => $vote_time,
                    'icon_title' => $icon_title
                );
            }
        }

        echo json_encode($result_arr);
        Yii::app()->end();
    }

    private function genArrayVote($point, $total)
    {
        $arr = array();
        $count_time = 0;
        while ($point >= 1 && $count_time <= $total)
        {
            $arr[] = 'full';
            $point-=1;
            $count_time+=1;
        }

        if ($point > 0)
        {
            $arr[] = 'half';
            $count_time+=1;
        }

        while ($count_time < 5)
        {
            $arr[] = 'blank';
            $count_time+=1;
        }
        return $arr;
    }

}

/** 
* End file SearchController
*/